<!--
 Copyright 2023 The CubeFS Authors.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 implied. See the License for the specific language governing
 permissions and limitations under the License.
-->

<template>
  <el-dialog
    :title="$t('common.permissions')"
    :visible.sync="dialogFormVisible"
    width="700px"
    @closed="clearData"
  >
    <el-form
      ref="form"
      :model="forms"
      :rules="rules"
      label-width="25%"
      class="mid-block"
    >
      <el-form-item :label="$t('common.volumename') + ':'" prop="volName">
        <el-input v-model="forms.volName" disabled class="input"></el-input>
      </el-form-item>
      <el-form-item :label="$t('common.tenant') + ':'" prop="user">
        <el-select
          v-model="forms.user"
          class="input"
          filterable
          reserve-keyword
          remote
          :placeholder="$t('volume.selectowner')"
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="item in userList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="子路径:" prop="childPath">
        <el-input
          v-model="forms.childPath"
          class="input"
          placeholder="请输入子路径"
        ></el-input>
      </el-form-item> -->
      <el-form-item :label="$t('common.permissions') + ':'" prop="auth">
        <el-checkbox-group v-model="forms.auth">
          <el-checkbox label="perm:builtin:ReadOnly">readonly</el-checkbox>
          <el-checkbox label="perm:builtin:Writable">writable</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button ref="pol" type="primary" @click="doCheck">{{ $t('button.submit') }}</el-button>
      <el-button ref="pol" type="primary" @click="close">{{ $t('button.cancel') }}</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { updateUserPolicy, getUserNameList } from '@/api/cfs/cluster'
import Mixin from '@/pages/cfs/clusterOverview/mixin'
export default {
  mixins: [Mixin],
  data() {
    return {
      userList: [],
      forms: {
        user: '',
        volName: '',
        childPath: '',
        auth: ['perm:builtin:Writable'],
      },
      dialogFormVisible: false,
    }
  },
  computed: {
    rules() {
      return {
        user: [
          {
            required: true,
            message: this.$t('usermgt.inputname'),
            trigger: 'blur',
          },
        ],
        auth: [
          {
            required: true,
            message: this.$t('volume.selectpermission'),
            trigger: 'blur',
          },
        ],
        volName: [
          {
            required: true,
            message: this.$t('volume.inputvolume'),
            trigger: 'blur',
          },
        ],
      }
    },
  },
  created() {},
  methods: {
    initForm(val) {
      this.forms = { ...this.forms, ...val }
    },
    open() {
      this.dialogFormVisible = true
      // 默认显示所有的用户
      this.getUserNameList()
    },
    clearData() {
      this.forms = {
        user: '',
        volName: '',
        childPath: '',
        auth: ['perm:builtin:Writable'],
      }
    },
    async doCheck() {
      await this.$refs.form.validate()
      const { user, volName, auth } = this.forms
      await updateUserPolicy({
        user_id: user,
        volume: volName,
        cluster_name: this.clusterName,
        policy: auth,
      })
      this.$message.success(this.$t('volume.grantsuc'))
      this.$emit('refresh')
      this.close()
    },
    close() {
      this.clearData()
      this.dialogFormVisible = false
    },
    async remoteMethod(query) {
      this.getUserNameList(query)
    },
    async getUserNameList(query) {
      const res = await getUserNameList({
        keywords: query,
        cluster_name: this.clusterName,
      })
      this.userList = (res.data || []).map((item) => {
        return {
          label: item,
          value: item,
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.input {
  width: 60%;
}
.dialog-footer {
  text-align: center;
}
</style>
